from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import Qt, QString

class MsgLED(QtGui.QWidget):
        def __init__(self, parent, side, colour, cover_img, *args, **kw):
		QtGui.QWidget.__init__(self, parent, *args, **kw)

		self.colour = colour
		self.on = True
		self.side = side

		self.bg = QtGui.QLabel(self)
		self.bg.setGeometry(1,1,side - 2,side - 2)
		self.set_shade(colour)

		cover = QtGui.QLabel(self)
		cover.setPixmap(QtGui.QPixmap(cover_img))
		cover.move(0,0)

	def set_shade(self, shade):
		ss = "background-color: {}".format(shade)
		self.bg.setStyleSheet(ss)

	def do_update(self):
		if self.on:
			self.set_shade(self.colour)
		else:
			self.set_shade("#333333")

	def turn_on(self):
		self.on = True
		self.do_update()

	def turn_off(self):
		self.on = False
		self.do_update()

class MiniLed(MsgLED):
        def __init__(self, parent, colour, *args, **kw):
		MsgLED.__init__(self,
			parent, 10, colour,
			"resources/mini_lamp_cover.png",
			*args, **kw
		)
		self.setGeometry(0, 0, 200, 32)

class MsgLamp(MsgLED):
        def __init__(self, parent, caption, colour, *args, **kw):
		MsgLED.__init__(self,
			parent, 32, colour,
			"resources/message_lamp_cover.png",
			*args, **kw
		)
		self.setGeometry(0, 0, 200, 32)

		self.caption = caption

		narrative = QtGui.QLabel(self)
		narrative.setStyleSheet("color: yellow")
		narrative.setText(caption)
		narrative.setGeometry(36,3, 200, 32)

		font = QtGui.QFont("Gabrielle", 24)
		narrative.setFont(font)

		self.do_update()

	def mousePressEvent(self, sender):
		self.on = not self.on
		self.set_state(self.on)

	def turn_on(self):
		self.set_state(True)

	def turn_off(self):
		self.set_state(False)

	def set_state(self, state):
		self.on = state
		self.do_update()
		self.toggled.emit(self.caption, state)

	toggled = QtCore.pyqtSignal(str, bool)

class MsgLamp_Button(MsgLamp):
        def __init__(self, parent, caption, *args, **kw):
		MsgLamp.__init__(self, parent, caption, "#333333", *args, **kw)

	def mousePressEvent(self, sender):
		self.set_shade("white")

	def mouseReleaseEvent(self, sender):
		self.set_shade("#333333")
		self.clicked.emit()

	clicked = QtCore.pyqtSignal()



if __name__ == "__main__":
	import sys
	app = QtGui.QApplication(sys.argv)

	font_db = QtGui.QFontDatabase()
	font_db.addApplicationFont("resources/Gabrielle.ttf")

#
#	use the wallpaper as the top level widget
#
	top = QtGui.QLabel()
	top.setGeometry(0, 0, 1024, 150)
	path = "resources/wood.jpg"
	top.setPixmap(QtGui.QPixmap(path))

	banner = QtGui.QLabel(top)
	banner.setGeometry(20, 20, 247, 86)
	path = "resources/messages_banner.png"
	banner.setPixmap(QtGui.QPixmap(path))

	def togg(what, how):
		print "{} set to {}".format(
			what, how
		)

	toggles = []

	xoff = 270
	lamp = MsgLamp(top, "Urgent", "red")
	lamp.move(xoff,20)
	lamp.toggled.connect(togg)
	toggles.append(lamp)

	lamp = MsgLamp(top, "Personal", "cyan")
	lamp.move(xoff,70)
	lamp.toggled.connect(togg)
	toggles.append(lamp)

	xoff += 160
	lamp = MsgLamp(top, "Tarts", "pink")
	lamp.move(xoff,20)
	lamp.toggled.connect(togg)
	toggles.append(lamp)

	lamp = MsgLamp(top, "Ruffians", "darkblue")
	lamp.move(xoff,70)
	lamp.toggled.connect(togg)
	toggles.append(lamp)

	xoff += 160
	lamp = MsgLamp(top, "Houses", "green")
	lamp.move(xoff,20)
	lamp.toggled.connect(togg)
	toggles.append(lamp)

	lamp = MsgLamp(top, "Adversaries", "yellow")
	lamp.move(xoff,70)
	lamp.toggled.connect(togg)
	toggles.append(lamp)

	xoff += 170
	lamp = MsgLamp(top, "News", "purple")
	lamp.move(xoff,20)
	lamp.toggled.connect(togg)
	toggles.append(lamp)

	lamp = MsgLamp(top, "Gossip", "darkorange")
	lamp.move(xoff,70)
	lamp.toggled.connect(togg)
	toggles.append(lamp)

	def all_off():
		for lamp in toggles:
			lamp.turn_off()

	def all_on():
		for lamp in toggles:
			lamp.turn_on()

	xoff += 130
	lamp = MsgLamp_Button(top, "All")
	lamp.move(xoff,20)
	lamp.clicked.connect(all_on)

	lamp = MsgLamp_Button(top, "None")
	lamp.move(xoff,70)
	lamp.clicked.connect(all_off)


	top.show()
	sys.exit(app.exec_())
	

